<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\Common\service;

class App
{
    private $uniacid;
    private $goods_name;
    private $base_url;
    private $check_url;
    private $uploadWxapp_url;
    private $get_auth_url;
    private $get_domain_param_url;
    private $http_host;
    private $server_name;
    private $request_time;
    private $public_key;
    private $domain_name_info;
    private $is_debug = false;
    private $token_path;
    public function __construct()
    {
    }
    public function baseUrl()
    {
    }
    public function checkUrl()
    {
    }
    public function getsAuthConfig()
    {
        $this->public_key = $this->getPublicKey();
        if(empty($this->public_key)) {
            return [];
        }
        $siginStr = $this->getSiginData([]);
        $result = $this->curl_post($this->get_domain_param_url, $this->getPostData($siginStr));
        $result = json_decode($result, true);
        $param_list = $result["data"]["param_list"];
        if(is_array($param_list)) {
            foreach ($param_list as $key => $item) {
                $param = "";
                openssl_public_decrypt(base64_decode($item), $param, $this->public_key);
                $param_list[$key] = $param;
            }
        }
        return $param_list;
    }
    public function getPostData($siginStr)
    {
        $postData = $this->getPublicPostData();
        $postData["sigin"] = $siginStr;
        return $postData;
    }
    public function getSiginData($extData = [], $siginType = 1)
    {
        $data = $this->getPublicPostData();
        if(!empty($extData)) {
            $data["ext_data"] = $extData;
        }
        ksort($data);
        $str_data = json_encode($data);
        if($siginType == 1) {
            @openssl_public_encrypt($str_data, $encrypted, $this->public_key);
            if(empty($encrypted)) {
                return false;
            }
            $encrypted = base64_encode($encrypted);
        } else {
            $encrypted = $this->getSiginDataByHash($data);
        }
        return $encrypted;
    }
    private function getSiginDataByHash($data)
    {
        $data["token"] = $data["token"] ? $data["token"] : "";
        $data = is_array($data) ? json_encode($data) : (is_string($data) ? $data : time() . "") . "LongbingShuixian";
        $siginStr = hash("sha256", $data);
        return $siginStr;
    }
    private function getPublicPostData()
    {
        $app_model_name = config("app.AdminModelList")["app_model_name"];
        $token = file_get_contents($this->token_path);
        $token = $token ? json_decode($token, true) : "";
        if(!empty($token)) {
            $token = $token["token"];
        }
        $data = ["uniacid" => $this->uniacid, "app_model_name" => $app_model_name, "goods_name" => $this->goods_name, "http_host" => $this->http_host, "server_name" => $this->server_name, "request_time" => $this->request_time, "token" => $token];
        return $data;
    }
    public function getCache()
    {
        $options = ["host" => \think\facade\Env::get("cache.host", "127.0.0.1"), "port" => \think\facade\Env::get("cache.port", 6379), "password" => \think\facade\Env::get("cache.passwd", ""), "expire" => \think\facade\Env::get("cache.expire", 0), "prefix" => \think\facade\Env::get("cache.prefix", "longbing_")];
        $redis = new \think\cache\driver\Redis($options);
        return $redis;
    }
    private function get_file($url, $folder = "./data/upgradex/")
    {
        set_time_limit(86400);
        $target_dir = $folder . "";
        if(!is_dir($target_dir)) {
            mkdir($target_dir, 511, true);
        }
        $newfname = date("Ymd") . rand(1000, 10000000) . uniqid() . ".zip";
        $newfname = $target_dir . $newfname;
        $file = @fopen($url, "rb");
        if($file) {
            $newf = fopen($newfname, "wb");
            if($newf) {
                while (!feof($file)) {
                    fwrite($newf, fread($file, 8192), 8192);
                }
            }
            fclose($file);
            if($newf) {
                fclose($newf);
            }
            return $newfname;
        }
        return false;
    }
    private function unzip($filename, $toFilepath, $password = NULL)
    {
        $zip = new ZipArchive();
        $res = $zip->open($filename);
        if($res === true) {
            $password && $zip->setPassword($password);
            $zip->extractTo($toFilepath);
            $zip->close();
        }
        return true;
    }
    public function getPublicKey()
    {
        if(!empty($this->public_key)) {
            return $this->public_key;
        }
        $siginStr = $this->getSiginData([], 2);
        $result = $this->curl_post($this->get_auth_url, $this->getPostData($siginStr));
        $result = json_decode($result, true);
        $this->domain_name_info = $result["data"]["domain_name_info"];
        $token = $result["data"]["token"];
        $resultWriteToken = $this->writein_token($token);
        $this->public_key = $result["data"]["public_key"];
        return $this->public_key;
    }
    public function curl_post($url, $data = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    private function writein_token($token)
    {
        $resultWriteToken = false;
        if(is_array($token)) {
            $resultWriteToken = file_put_contents($this->token_path, json_encode($token));
        } else {
            $token = @file_get_contents($this->token_path);
            $token = $token ? json_decode($token, true) : "";
            if(!empty($token) && $token["token_expiration_time"] < time()) {
                $token["token"] = "";
                $resultWriteToken = file_put_contents($this->token_path, json_encode($token));
            }
        }
        return $resultWriteToken ? true : false;
    }
}

?>